﻿
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Net.Http.Headers;
using Newtonsoft.Json.Serialization;
namespace BlazorSystem.Server.Controllers
{

    [ApiController]
    [Route("[controller]")]
    public class LoginController : ControllerBase
    {
        private readonly IConfiguration _configuration;
        private readonly ILogger<LoginController> _logger;

        public LoginController(ILogger<LoginController> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }

        [HttpPost(Name = "Login")]
        public async Task<ActionResult<string>> Post()
        {
            try
            {

                StreamReader requestReader = new StreamReader(Request.Body);
                string rawRequestBody = await requestReader.ReadToEndAsync();

                // 解析JSON字符串
                List<string> data = YZZ.工应.WebAPI.JsonToList(rawRequestBody);
                string userName = data[0].Replace('\"', ' ').Trim();
                string passWord = data[1].Replace('\"', ' ').Trim();
                //passWord = YZZ.工应.WebAPI.解密(passWord, Program.Key);
                string Return = Program.LoginData.CheckLogin(userName, passWord);
                if (Return.Split(':').Length > 1)
                    Return = Return.Split(':')[1];
                return Ok(Return);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "登录时出现异常");
                return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
            }
        }

    }
}
